﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Linq;
using System.Windows.Forms;

namespace CommonLib
{
    public class XMLConfig
    {
        //private string m_path = System.Environment.CurrentDirectory; //bin\debug\
        private string m_path = Application.StartupPath;           //bin\debug\
        /// <summary>
        /// 读取xml配置文件
        /// </summary>
        /// <param name="xmlConfigFileName">xml配置文件名，以Configs为相对路径</param>
        public XMLConfig(string xmlConfigFileName)
        {
            m_path = m_path.Substring(0, m_path.LastIndexOf("bin")) + "Configs\\" + xmlConfigFileName;
            //FileStream fileStream = new FileStream(m_path, FileMode.Open);
            //StreamReader reader=new StreamReader(fileStream,Encoding.Default);
            //string xmlConfig=reader.ReadToEnd();
            //XmlReader xmlReader = XmlReader.Create(new StringReader(xmlConfig));
            //XDocument document = XDocument.Load(xmlReader);
            //XElement[] configArray = document.Descendants("config").ToArray<XElement>();
            //string name = null;
            //string value = null;
            //for (int i = 0; i < configArray.GetLength(0); i++)
            //{
            //    name = configArray[i].Attribute("name").Value.ToString();
            //    value = configArray[i].Attribute("value").Value.ToString();
            //    m_dir.Add(name, value);
            //}
            //xmlReader.Close();
            //reader.Close();
            //fileStream.Close();
        }
        public XMLConfig(string xmlConfigFileName, bool fullname)
        {
            if (fullname)
            {
                m_path = xmlConfigFileName;
            }
            else
            {
                m_path = m_path.Substring(0, m_path.LastIndexOf("bin")) + "Configs\\" + xmlConfigFileName;
            }
            //FileStream fileStream = new FileStream(m_path, FileMode.Open);
            //StreamReader reader=new StreamReader(fileStream,Encoding.Default);
            //string xmlConfig=reader.ReadToEnd();
            //XmlReader xmlReader = XmlReader.Create(new StringReader(xmlConfig));
            //XDocument document = XDocument.Load(xmlReader);
            //XElement[] configArray = document.Descendants("config").ToArray<XElement>();
            //string name = null;
            //string value = null;
            //for (int i = 0; i < configArray.GetLength(0); i++)
            //{
            //    name = configArray[i].Attribute("name").Value.ToString();
            //    value = configArray[i].Attribute("value").Value.ToString();
            //    m_dir.Add(name, value);
            //}
            //xmlReader.Close();
            //reader.Close();
            //fileStream.Close();
        }
        //例如：
        //求:“主地图数据路径”的值
        //解：GetAttributeValueFromSpcialAttribute("AppConfig","name","主地图数据路径","value")
        //返回："MapData\主地图.mxd"
        //<AppConfig>
        //  <config name="主地图数据路径" value="MapData\主地图.mxd" />
        //  <config name="三维Sxd数据路径" value="MapData\tin\三维图ArcScene.sxd" />
        //</AppConfig>
        /// <summary>
        /// 指定节点名下，获取“属性名=属性值”节点中，指定属性名的属性值
        /// </summary>
        /// <param name="nodeName">节点名</param>
        /// <param name="inAttributeName">属性名（输入）</param>
        /// <param name="inAttributeValue">属性值（输入）</param>
        /// <param name="outAttributeName">属性名（输出）</param>
        /// <returns>属性值（输出）</returns>
        public string GetAttributeValueFromSpcialAttribute(string nodeName, string inAttributeName, string inAttributeValue, string outAttributeName)
        {
            FileStream fileStream = new FileStream(m_path, FileMode.Open);
            StreamReader reader = new StreamReader(fileStream, Encoding.Default);
            string xmlConfig = reader.ReadToEnd();
            XmlReader xmlReader = XmlReader.Create(new StringReader(xmlConfig));
            XDocument document = XDocument.Load(xmlReader);
            XElement[] configArray = document.Descendants(nodeName).ToArray<XElement>();
            string outAttributeValue = null;

            for (int i = 0; i < configArray.GetLength(0); i++)
            {
                if (configArray[i].Attribute(inAttributeName).Value.ToString().Trim() == inAttributeValue)
                {
                    outAttributeValue = configArray[i].Attribute(outAttributeName).Value.ToString();
                    break;
                }
            }
            xmlReader.Close();
            reader.Close();
            fileStream.Close();

            return outAttributeValue;
        }
        //例如：
        //求:“主地图数据路径”的值
        //解：GetAttributeValueFromSpcialAttribute("AppConfig","name","主地图数据路径","value")
        //返回："MapData\主地图.mxd"
        //<AppConfig>
        //  <config name="主地图数据路径" value="MapData\主地图.mxd" />
        //  <config name="三维Sxd数据路径" value="MapData\tin\三维图ArcScene.sxd" />
        //</AppConfig>
        /// <summary>
        /// 指定节点名下，获取“属性名=属性值”节点中，指定属性名的属性值列表
        /// </summary>
        /// <param name="nodeName">节点名</param>
        /// <param name="inAttributeName">属性名（输入）</param>
        /// <param name="inAttributeValue">属性值（输入）</param>
        /// <param name="outAttributeName">属性名（输出）</param>
        /// <returns>属性值列表（输出）</returns>
        public List<string> GetAttributeValueListFromSpcialAttribute(string nodeName, string inAttributeName, string inAttributeValue, string outAttributeName)
        {
            FileStream fileStream = new FileStream(m_path, FileMode.Open);
            StreamReader reader = new StreamReader(fileStream, Encoding.Default);
            string xmlConfig = reader.ReadToEnd();
            XmlReader xmlReader = XmlReader.Create(new StringReader(xmlConfig));
            XDocument document = XDocument.Load(xmlReader);
            XElement[] configArray = document.Descendants(nodeName).ToArray<XElement>();
            string outAttributeValue = null;

            List<string> ret = new List<string>();

            for (int i = 0; i < configArray.GetLength(0); i++)
            {
                if (configArray[i].Attribute(inAttributeName).Value.ToString().Trim() == inAttributeValue)
                {
                    outAttributeValue = configArray[i].Attribute(outAttributeName).Value.ToString();
                    ret.Add(outAttributeValue);
                }
            }
            xmlReader.Close();
            reader.Close();
            fileStream.Close();

            return ret;
        }
        //例如：
        //求:所有图层的图层名列表（List）
        //解：GetAttributeValueListFromAttributeName("Layer","name")
        //返回："分等单元"，"自然质量等"
        // <AppConfig>
        //    <Layer name="分等单元"></Layer>
        //    <Layer name="自然质量等"></Layer>
        //</AppConfig>
        /// <summary>
        /// 指定节点名下，获取指定属性名的所有属性值
        /// </summary>
        /// <param name="nodeName">节点名</param>
        /// <param name="attributeName">属性名</param>
        /// <returns>属性值List</returns>
        public List<string> GetAttributeValueListFromAttributeName(string nodeName, string attributeName)
        {
            FileStream fileStream = new FileStream(m_path, FileMode.Open);
            StreamReader reader = new StreamReader(fileStream, Encoding.Default);
            string xmlConfig = reader.ReadToEnd();
            XmlReader xmlReader = XmlReader.Create(new StringReader(xmlConfig));
            XDocument document = XDocument.Load(xmlReader);
            XElement[] configArray = document.Descendants(nodeName).ToArray<XElement>();
            List<string> attributeValueList = new List<string>();

            for (int i = 0; i < configArray.GetLength(0); i++)
            {
                attributeValueList.Add(configArray[i].Attribute(attributeName).Value.ToString());
            }
            xmlReader.Close();
            reader.Close();
            fileStream.Close();

            return attributeValueList;
        }

        //例如：
        //求:“分等单元”图层（Layer）中，所有字段（field）的name值或aliasname值
        //解：GetAttributeValueListFromNodeName("Layer","name","分等单元","Field","name")
        //返回："一级区名称"，"二级区名称"
        // <AppConfig>
        //    <Layer name="分等单元">
        //        <Field name="一级区名称" aliasname="一级区名称"></Field>
        //        <Field name="二级区名称" aliasname="二级区名称"></Field>
        //    </Layer>
        //    <Layer name="自然质量等">
        //        <Field name="等级" aliasname="等级"></Field>
        //        <Field name="区位" aliasname="区位"></Field>
        //    </Layer>
        //</AppConfig>
        /// <summary>
        /// 指定父节点下，属性名=属性值
        /// 获取当前节点名下，指定属性名的所有属性值
        /// </summary>
        /// <param name="parentNodeName">父节点名</param>
        /// <param name="parentAttributeName">父节点属性名</param>
        /// <param name="parentAttributeValue">父节点属性值</param>
        /// <param name="curNodeName">当前节点名</param>
        /// <param name="curAttributeName">当前节点属性名</param>
        /// <returns></returns>
        public List<string> GetAttributeValueListFromNodeName(string parentNodeName, string parentAttributeName, string parentAttributeValue, string curNodeName, string curAttributeName)
        {
            List<string> attributeValueList = new List<string>();
            FileStream fileStream = new FileStream(m_path, FileMode.Open);
            StreamReader reader = new StreamReader(fileStream, Encoding.Default);
            string xmlConfig = reader.ReadToEnd();
            XmlReader xmlReader = XmlReader.Create(new StringReader(xmlConfig));
            XDocument document = XDocument.Load(xmlReader);
            XElement[] parentNodeArray = document.Descendants(parentNodeName).ToArray<XElement>();
            for (int i = 0; i < parentNodeArray.GetLength(0); i++)
            {
                if (parentNodeArray[i].Attribute(parentAttributeName).Value.ToString() == parentAttributeValue)
                {
                    XElement[] curNodeArray = parentNodeArray[i].Descendants(curNodeName).ToArray<XElement>();
                    foreach (XElement curNode in curNodeArray)
                    {
                        attributeValueList.Add(curNode.Attribute(curAttributeName).Value.ToString());
                    }
                    break;
                }

            }
            xmlReader.Close();
            reader.Close();
            fileStream.Close();

            return attributeValueList;
        }

        //例如：
        //求:“分等单元”图层（Layer）中，type="aaa"的所有字段（field）的name值或aliasname值
        //解：GetAttributeValueListFromNodeName("Layer","name","分等单元","Field","name","type","aaa")
        //返回："一级区名称"，"三级区名称"
        // <AppConfig>
        //    <Layer name="分等单元">
        //        <Field name="一级区名称" type="aaa"></Field>
        //        <Field name="二级区名称" type="bbb"></Field>
        //        <Field name="三级区名称" type="aaa"></Field>
        //    </Layer>
        //    <Layer name="自然质量等">
        //        <Field name="等级" aliasname="等级"></Field>
        //        <Field name="区位" aliasname="区位"></Field>
        //    </Layer>
        //</AppConfig>
        /// <summary>
        /// 指定父节点下，属性名=属性值
        /// 获取当前节点名下，指定属性名的所有属性值
        /// </summary>
        /// <param name="parentNodeName">父节点名</param>
        /// <param name="parentAttributeName">父节点属性名</param>
        /// <param name="parentAttributeValue">父节点属性值</param>
        /// <param name="curNodeName">当前节点名</param>
        /// <param name="curAttributeName">当前节点属性名</param>
        /// <returns></returns>
        public List<string> GetAttributeValueListFromNodeNameWithSpecialAttribute(string parentNodeName, string parentAttributeName, string parentAttributeValue, string curNodeName, string curAttributeName, string curSpecialFieldName, string curSpecialFieldValue)
        {
            List<string> attributeValueList = new List<string>();
            FileStream fileStream = new FileStream(m_path, FileMode.Open);
            StreamReader reader = new StreamReader(fileStream, Encoding.Default);
            string xmlConfig = reader.ReadToEnd();
            XmlReader xmlReader = XmlReader.Create(new StringReader(xmlConfig));
            XDocument document = XDocument.Load(xmlReader);
            XElement[] parentNodeArray = document.Descendants(parentNodeName).ToArray<XElement>();
            for (int i = 0; i < parentNodeArray.GetLength(0); i++)
            {
                if (parentNodeArray[i].Attribute(parentAttributeName).Value.ToString() == parentAttributeValue)
                {
                    XElement[] curNodeArray = parentNodeArray[i].Descendants(curNodeName).ToArray<XElement>();
                    foreach (XElement curNode in curNodeArray)
                    {
                        if (curNode.Attribute(curSpecialFieldName).Value.ToString() == curSpecialFieldValue)
                            attributeValueList.Add(curNode.Attribute(curAttributeName).Value.ToString());
                    }
                    break;
                }

            }
            xmlReader.Close();
            reader.Close();
            fileStream.Close();

            return attributeValueList;
        }

        public string GetAttributeValueFromNodeNameWithSpecialAttribute(string parentNodeName, string parentAttributeName, string parentAttributeValue, string curNodeName, string curAttributeName, string curSpecialFieldName, string curSpecialFieldValue,string x1,string x2)
        {
            string attributeValue = "";
            FileStream fileStream = new FileStream(m_path, FileMode.Open);
            StreamReader reader = new StreamReader(fileStream, Encoding.Default);
            string xmlConfig = reader.ReadToEnd();
            XmlReader xmlReader = XmlReader.Create(new StringReader(xmlConfig));
            XDocument document = XDocument.Load(xmlReader);
            XElement[] parentNodeArray = document.Descendants(parentNodeName).ToArray<XElement>();
            for (int i = 0; i < parentNodeArray.GetLength(0); i++)
            {
                if (parentNodeArray[i].Attribute(parentAttributeName).Value.ToString() == parentAttributeValue)
                {
                    XElement[] curNodeArray = parentNodeArray[i].Descendants(curNodeName).ToArray<XElement>();
                    foreach (XElement curNode in curNodeArray)
                    {
                        if (curNode.Attribute(curSpecialFieldName).Value.ToString() == curSpecialFieldValue)
                        {
                            if (curNode.Attribute(x1).Value.ToString() == x2)
                            {
                                attributeValue = curNode.Attribute(curAttributeName).Value.ToString();
                                break;
                            }
                        }
                    }
                }

            }
            xmlReader.Close();
            reader.Close();
            fileStream.Close();

            return attributeValue;
        }
        /// <summary>
        /// 添加节点
        /// </summary>
        /// <param name="strName">name属性值</param>
        /// <param name="strValue">value属性值</param>
        public void SetData(string strName, string strValue)
        {
            FileStream fileStream = new FileStream(m_path, FileMode.Open);
            StreamReader reader = new StreamReader(fileStream, Encoding.Default);
            string xmlConfig = reader.ReadToEnd();
            XmlReader xmlReader = XmlReader.Create(new StringReader(xmlConfig));
            XDocument document = XDocument.Load(xmlReader);
            XElement[] configArray = document.Descendants("config").ToArray<XElement>();
            xmlReader.Close();
            reader.Close();
            fileStream.Close();
            string name = null;
            string value = null;
            for (int i = 0; i < configArray.GetLength(0); i++)
            {
                name = configArray[i].Attribute("name").Value.ToString();
                if (name != strName) continue;
                configArray[i].Attribute("value").Value = strValue;
                break;
            }
            StreamWriter xmlTextWrite = new StreamWriter(m_path, false, Encoding.UTF8);
            xmlTextWrite.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            xmlTextWrite.WriteLine("<AppConfig>");
            for (int i = 0; i < configArray.GetLength(0); i++)
            {
                name = configArray[i].Attribute("name").Value.ToString();
                value = configArray[i].Attribute("value").Value.ToString();
                xmlTextWrite.WriteLine("  <config name=\"" + name + "\" value=\"" + value + "\" />");
            }
            xmlTextWrite.WriteLine("</AppConfig>");
            xmlTextWrite.Close();
        }

        public void SetCustomSkin(string skinName)
        {
            FileStream fileStream = new FileStream(m_path, FileMode.Open);
            StreamReader reader = new StreamReader(fileStream, Encoding.Default);
            string xmlConfig = reader.ReadToEnd();
            XmlReader xmlReader = XmlReader.Create(new StringReader(xmlConfig));
            XDocument document = XDocument.Load(xmlReader);
            XElement[] configArray = document.Descendants("Skin").ToArray<XElement>();
            xmlReader.Close();
            reader.Close();
            fileStream.Close();
            if (configArray[0].Attribute("value").Value.ToString().Equals(skinName)) return;
            StreamWriter xmlTextWrite = new StreamWriter(m_path, false, Encoding.UTF8);
            xmlTextWrite.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            xmlTextWrite.WriteLine("<AppConfig>");
            xmlTextWrite.WriteLine("<Skin name=\" CustomSkinName \" value=\"" + skinName + "\" />");
            xmlTextWrite.WriteLine("</AppConfig>");
            xmlTextWrite.Close();
        }

        /// <summary>
        /// 添加目录
        /// </summary>
        /// <param name="strValue">路径值</param>
        public void AddDirPathInfo(string ParentNodeName, List<string> ChildNodeNameList, List<string> ChildValueList,string baseFun)
        {

            if (ChildNodeNameList.Count != ChildValueList.Count) return;
            StreamWriter xmlTextWrite = new StreamWriter(m_path, false, Encoding.UTF8);
            xmlTextWrite.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            xmlTextWrite.WriteLine("<AppConfig>");
            for (int i = 0; i < ChildNodeNameList.Count; i++)
            {
                //格式：<Folder Type="ExtendMapDataFolder" value="" baseFun="JCPJ"></Folder>
                xmlTextWrite.WriteLine("<" + ParentNodeName + " Type = \"" + ChildNodeNameList[i] + "\" Value= \"" + ChildValueList[i] + "\" BaseFun= \"" + baseFun + "\"/>");
            }
            xmlTextWrite.WriteLine("</AppConfig>");
            xmlTextWrite.Close();
        }
        /// <summary>
        /// 添加登录配置信息
        /// </summary>
        /// <param name="loginConfigInfoArray"></param>
        public void  AddLoginConfigInfo(string [,] loginConfigInfoArray)
        {

            if (loginConfigInfoArray==null||loginConfigInfoArray.GetLength(1)!=3) return;
            StreamWriter xmlTextWrite = new StreamWriter(m_path, false, Encoding.UTF8);
            xmlTextWrite.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            xmlTextWrite.WriteLine("<AppConfig>");
            for (int i = 0; i < loginConfigInfoArray.GetLength(0); i++)
            {
                xmlTextWrite.WriteLine("<Login type= \"" + loginConfigInfoArray[i, 0].ToString() + "\" value= \"" + loginConfigInfoArray[i, 1].ToString() + "\" CheckState= \"" + loginConfigInfoArray[i, 2].ToString() + "\" />");
            }
            xmlTextWrite.WriteLine("</AppConfig>");
            xmlTextWrite.Close();
            //FileStream fileStream = new FileStream(m_path, FileMode.Open);
            //StreamReader reader = new StreamReader(fileStream, Encoding.Default);
            //string xmlConfig = reader.ReadToEnd();
            //XmlReader xmlReader = XmlReader.Create(new StringReader(xmlConfig));
            //XDocument document = XDocument.Load(xmlReader);
            //XElement[] configArray = document.Descendants("Path").ToArray<XElement>();
            //xmlReader.Close();
            //reader.Close();
            //fileStream.Close();
            //StreamWriter xmlTextWrite = new StreamWriter(m_path, false, Encoding.UTF8);
            //xmlTextWrite.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            //xmlTextWrite.WriteLine("<AppConfig>");
            //xmlTextWrite.WriteLine("<Path name=\" 原始数据 \" value=\"" + strValue1 + "\" />");
            //xmlTextWrite.WriteLine("<Path name=\" 成果数据 \" value=\"" + strValue2 + "\" />");
            //xmlTextWrite.WriteLine("</AppConfig>");
            //xmlTextWrite.Close();
        }
        /// <summary>
        /// 获取所有nodename的节点
        /// </summary>
        /// <param name="nodeName"></param>
        /// <returns></returns>
        public XElement[] GetSpecialNodes(string nodeName)
        {
            FileStream fileStream = new FileStream(m_path, FileMode.Open);
            StreamReader reader = new StreamReader(fileStream, Encoding.Default);
            string xmlConfig = reader.ReadToEnd();
            XmlReader xmlReader = XmlReader.Create(new StringReader(xmlConfig));
            XDocument document = XDocument.Load(xmlReader);
            XElement[] configArray = document.Descendants(nodeName).ToArray<XElement>();

            xmlReader.Close();
            reader.Close();
            fileStream.Close();

            return configArray;
        }

        /// <summary>
        /// 添加历史路径
        /// </summary>
        /// <param name="strValue">路径值</param>
        public void AddHistoryDictionaryPathInfo(List<string> pathList)
        {
            StreamWriter xmlTextWrite = new StreamWriter(m_path, false, Encoding.UTF8);
            xmlTextWrite.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            xmlTextWrite.WriteLine("<AppConfig>");
            if (pathList.Count <= 5)
            {
                for (int i = 0; i < pathList.Count; i++)
                {
                    xmlTextWrite.WriteLine("  <DictionaryPathInfo name=\" 历史路径 \" value=\"" + pathList[i] + "\" />");
                }
            }
            else
            {
                for (int i = 0; i < 5; i++)
                {
                    xmlTextWrite.WriteLine("  <DictionaryPathInfo name=\" 历史路径 \" value=\"" + pathList[i] + "\" />");
                }
            }
            xmlTextWrite.WriteLine("</AppConfig>");
            xmlTextWrite.Close();
        }

        public void SaveCustomConfig(string skinName, string min, string max, string add, string gc)
        {
            FileStream fileStream = new FileStream(m_path, FileMode.Open);
            StreamReader reader = new StreamReader(fileStream, Encoding.Default);
            string xmlConfig = reader.ReadToEnd();
            XmlReader xmlReader = XmlReader.Create(new StringReader(xmlConfig));
            XDocument document = XDocument.Load(xmlReader);
            XElement[] configArray = document.Descendants("config").ToArray<XElement>();
            xmlReader.Close();
            reader.Close();
            fileStream.Close();
            //if (configArray[0].Attribute("value").Value.ToString().Equals(skinName)) return;
            StreamWriter xmlTextWrite = new StreamWriter(m_path, false, Encoding.UTF8);
            xmlTextWrite.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            xmlTextWrite.WriteLine("<AppConfig>");
            xmlTextWrite.WriteLine("<config name=\" Skin \" value=\"" + skinName + "\" />");
            xmlTextWrite.WriteLine("<config name=\" Min \" value=\"" + min + "\" />");
            xmlTextWrite.WriteLine("<config name=\" Max \" value=\"" + max + "\" />");
            xmlTextWrite.WriteLine("<config name=\" Add \" value=\"" + add + "\" />");
            xmlTextWrite.WriteLine("<config name=\" GC \" value=\"" + gc + "\" />");
            xmlTextWrite.WriteLine("</AppConfig>");
            xmlTextWrite.Close();
        }
        /// <summary>
        /// 添加历史路径
        /// </summary>
        /// <param name="strValue">路径值</param>
        public void AddHistoryDictionaryPathInfo(string path)
        {
            StreamWriter xmlTextWrite = new StreamWriter(m_path, false, Encoding.UTF8);
            xmlTextWrite.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            xmlTextWrite.WriteLine("<AppConfig>");
            xmlTextWrite.WriteLine("  <config name=\" HistoryWorkSpacePath \" value=\"" + path + "\" />");
            xmlTextWrite.WriteLine("</AppConfig>");
            xmlTextWrite.Close();
        }

        public void AddCurrentFilePathInfo(string mdbpath, string shppath)
        {
            StreamWriter xmlTextWrite = new StreamWriter(m_path, false, Encoding.UTF8);
            xmlTextWrite.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            xmlTextWrite.WriteLine("<AppConfig>");
            xmlTextWrite.WriteLine("  <config name=\" CurrentFile \" MdbfileValue=\"" + mdbpath + "\"  ShpfileValue=\"" + shppath + "\" />");
            xmlTextWrite.WriteLine("</AppConfig>");
            xmlTextWrite.Close();
        }

        public void EditXmlFile(string rootNodeName, string childNodeName1, string filterNodeValue,string setValueNode, string setValue)
        {
            ////<Folder Type = "ExtendMapDataFolder" Value= "E:\0_耕地质量动态监测系统\耕地质量监测评价信息系统\StartUp\MapData\ExtendData\JCPJ" BaseFun= "JCPJ"/>
            //XDocument xdoc = new XDocument(m_path);
            //xdoc.Element(rootNodeName).Element(parentNodeName).Attribute(childNodeName).Value = value;

            XmlDocument doc = new XmlDocument();
            doc.Load(m_path);
            XmlNodeList nodeList = doc.SelectNodes("/AppConfig/" + rootNodeName);
            foreach (XmlNode node in nodeList)
            {

                string childNodeNameValue1 = node.Attributes[childNodeName1].Value.ToString();
                if (childNodeNameValue1.Equals(filterNodeValue))
                {
                    node.Attributes[setValueNode].Value = setValue;
                }
            }
            doc.Save(m_path);
        }

        /// <summary>
        /// 添加目录
        /// </summary>
        /// <param name="strValue">路径值</param>
        public void AddDirPathInfo(string ParentNodeName, List<KeyValuePair<string, string>> ChildNodeNameList, List<string> ChildValueList,List<string>LayerNameList)
        {

            if (ChildNodeNameList.Count != ChildValueList.Count) return;
            StreamWriter xmlTextWrite = new StreamWriter(m_path, false, Encoding.UTF8);
            xmlTextWrite.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            xmlTextWrite.WriteLine("<AppConfig>");
            for (int i = 0; i < ChildNodeNameList.Count; i++)
            {
                xmlTextWrite.WriteLine("<" + ParentNodeName + " name= \"" + ChildNodeNameList[i].Key + "\" value= \"" + ChildValueList[i] + "\" chsName= \"" + ChildNodeNameList[i].Value + "\" layer= \"" + LayerNameList[i] + "\" />");
            }
            xmlTextWrite.WriteLine("</AppConfig>");
            xmlTextWrite.Close();
        }



        /// <summary>
        /// 添加目录
        /// </summary>
        /// <param name="strValue">路径值</param>
        public Dictionary<string, string> GetNamePairAlisaNameDic(string nodeName, string attributeName1, string attributeName2)
        {
            FileStream fileStream = new FileStream(m_path, FileMode.Open);
            StreamReader reader = new StreamReader(fileStream, Encoding.Default);
            string xmlConfig = reader.ReadToEnd();
            XmlReader xmlReader = XmlReader.Create(new StringReader(xmlConfig));
            XDocument document = XDocument.Load(xmlReader);
            XElement[] configArray = document.Descendants(nodeName).ToArray<XElement>();
            Dictionary<string, string> attributeValueDic = new Dictionary<string, string>();

            for (int i = 0; i < configArray.GetLength(0); i++)
            {
                attributeValueDic.Add(configArray[i].Attribute(attributeName1).Value.ToString(), configArray[i].Attribute(attributeName2).Value.ToString());
            }
            xmlReader.Close();
            reader.Close();
            fileStream.Close();
            return attributeValueDic;
        }

        /// <summary>
        /// 根据路径、节点；获得节点下面子节点信息
        /// </summary>
        /// <param name="xmlConfigFileName"></param>
        /// <param name="node"></param>
        /// <returns></returns>
        public static List<string> getBDDJInfo(string xmlConfigFileName,string node)
        {
            string path = Application.StartupPath;
            string m_path = path.Substring(0, path.LastIndexOf("bin")) + "Configs\\" + xmlConfigFileName;
            List<string> list = new List<string>();
            XmlDocument doc = new XmlDocument();
            doc.Load(m_path);
            XmlNode xn = doc.SelectSingleNode(node);

            XmlNodeList xnl =  xn.ChildNodes;

            foreach (XmlNode xn1 in xnl)
            {
                list.Add(xn1.InnerText);
            }
            return list;
        }

        public static string getLayerByMXDAndLayerConfig(string xmlConfigFileName, string node,string attribute)
        {
            string path = Application.StartupPath;
            string m_path = path.Substring(0, path.LastIndexOf("bin")) + "Configs\\" + xmlConfigFileName;
            
            XmlDocument doc = new XmlDocument();
            doc.Load(m_path);
            XmlNode xn = doc.SelectSingleNode(node);
            string str = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);//xn.InnerText;
            return str;
        }
    }
}
